package com.trkj.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trkj.dao.jpa.IBusPeopleDao;
import com.trkj.dao.jpa.IBusinessDao;
import com.trkj.dao.mybatis.IBusPeopleMapper;
import com.trkj.dao.mybatis.IBusinessMapper;
import com.trkj.pojos.BusPeople;
import com.trkj.pojos.Business;

@Service
@Transactional
public class BusinessService {
	@Autowired
	private IBusinessDao businessDao;
	@Autowired
	private IBusPeopleDao busPeopleDao;
	@Autowired
	private IBusinessMapper businessMapper;
	@Autowired
	private IBusPeopleMapper busPeopleMapper;
	/**
	 * 保存外包商
	 * @param business
	 * @param busPeople
	 * @return
	 */
	public boolean addBusiness(Business business, List<BusPeople> busPeople){
		try {
			businessDao.save(business);		
			//绑定关系
			if(busPeople != null){
				for(BusPeople people: busPeople){
					people.setBusiness(business);
				}
			}		
			busPeopleDao.save(busPeople);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 查询外包商分页
	 * 
	 */
	public Map<String,Object> getBusiness(int pageNum,int pageSize){
		Page<Business> p = PageHelper.startPage(pageNum, pageSize, true);
		List<Business> list = businessMapper.findBusinessAll();
		
		Map<String,Object> map = new HashMap<>();
		map.put("total", p.getTotal());
		map.put("rows", list);
		return map;
	}
	/**
	 * 条件查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public Map<String,Object> getBusinessWhere(int pageNum,int pageSize,String busType,String busName,String busAddress){
		Page<Business> p = PageHelper.startPage(pageNum, pageSize, true);
		Map<String,Object> where= new HashMap<>();
		where.put("busType", busType);
		where.put("busName", busName);
		where.put("busAddress", busAddress);
		
		List<Business> list = businessMapper.selectBusinessWhere(where);
		
		Map<String,Object> map = new HashMap<>();
		map.put("total", p.getTotal());
		map.put("rows", list);
		return map;
	}
	/**
	 * 删除外包商
	 * @param eplan_id
	 * @return 
	 * @return
	 */

	public boolean deleteBusiness(long busId){
			try {
				Business one = businessDao.findOne(busId);	
				//绑定关系
				if(one.getBusPeoples() != null){
					List<BusPeople> busPeoples = one.getBusPeoples();
					for (BusPeople busPeople : busPeoples) {
						busPeopleDao.delete(busPeople);
					}
				}
				businessDao.delete(one);
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
	}
	/**
	 * 查询一个外包商的联系人详情
	 * @param businessId
	 * @return
	 */
	public Map<String, Object> BusinessOnePeople(long businessId) {	
		Map<String,Object> where = new HashMap<>();
		where.put("busId", businessId);
		List<BusPeople> people = busPeopleMapper.findBusPeoples(where);
		Map<String,Object> map= new HashMap<>();
		map.put("total", people.size());
		map.put("rows", people);
		return map;
	}
	/**
	 * 编辑外商
	 * @param busId
	 * @param business
	 * @param peoples
	 * @return
	 */
	public boolean editeBusiness(long busId,Business business, List<BusPeople> peoples) {
		try{
			Business one = businessDao.findOne(busId);
			one.setBusName(business.getBusName());
			one.setBusPhone(business.getBusPhone());
			one.setBusType(business.getBusType());
			one.setBusAddress(business.getBusAddress());
			
			List<BusPeople> busPeoples = one.getBusPeoples();
			for (BusPeople busPeople : busPeoples) {
				busPeopleDao.delete(busPeople);
			}
			if(peoples!=null){
				for (BusPeople busPeople : peoples) {
					busPeople.setBusiness(one);
				}
			}
			busPeopleDao.save(peoples);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 查询供应商分页
	 * 
	 */
	public Map<String,Object> getSupplierBusiness(int pageNum,int pageSize){
		Page<Business> p = PageHelper.startPage(pageNum, pageSize, true);
		List<Business> list = businessMapper.findSupplierBusiness();
		
		Map<String,Object> map = new HashMap<>();
		map.put("total", p.getTotal());
		map.put("rows", list);
		return map;
	}

}
